import { ViewRect, WebViewMsgEntity } from '../../entity/WorkerMsgEntity';

import worker, { ThreadWorkerGlobalScope } from '@ohos.worker';

export class WebViewWorkerMsgHandler {
    private static workerPort :ThreadWorkerGlobalScope;

    static init(workerPort) : void {
        WebViewWorkerMsgHandler.workerPort = workerPort;
    }

    static getWorkerPort():ThreadWorkerGlobalScope{
        if(WebViewWorkerMsgHandler.workerPort == null){
            WebViewWorkerMsgHandler.workerPort = worker.workerPort
        }
        return WebViewWorkerMsgHandler.workerPort;
    }

    static createWebView(viewTag: number, x: number, y: number, w: number, h: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('createWebView', viewTag);
        let viewInfo: ViewRect = new ViewRect(x, y, w, h);
        webViewMsgEntity.viewRect = viewInfo;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static removeWebView(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('removeWebView', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static loadData(viewTag: number, data: string, mimeType: string, encoding: string, baseURL: string) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('loadData', viewTag);
        webViewMsgEntity.data = data;
        webViewMsgEntity.mimeType = mimeType;
        webViewMsgEntity.encoding = encoding;
        webViewMsgEntity.baseURL = baseURL;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static loadURL(viewTag: number, url: string) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('loadURL', viewTag);
        webViewMsgEntity.url = url;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static loadFile(viewTag: number, filePath: string) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('loadFile', viewTag);
        webViewMsgEntity.filePath = filePath;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static stopLoading(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('stopLoading', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static reload(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('reload', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static canGoBack(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('canGoBack', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static canGoForward(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('canGoForward', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static goBack(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('goBack', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static goForward(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('goForward', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static setWebViewRect(viewTag: number, x: number, y: number, w: number, h: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('setWebViewRect', viewTag);
        let viewRect: ViewRect = new ViewRect(x, y, w, h);
        webViewMsgEntity.viewRect = viewRect;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static setVisible(viewTag: number, visible: boolean) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('setVisible', viewTag);
        webViewMsgEntity.visible = visible;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static setOpacityWebView(viewTag: number, opacity: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('setOpacityWebView', viewTag);
        webViewMsgEntity.opacity = opacity;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static setBackgroundTransparent(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('setBackgroundTransparent', viewTag);
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static evaluateJS(viewTag: number, js: string) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('evaluateJS', viewTag);
        webViewMsgEntity.js = js;
        WebViewWorkerMsgHandler.getWorkerPort().postMessage(webViewMsgEntity);
    }

    static setScalesPageToFit(viewTag: number, scalesPageToFit: boolean) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('setScalesPageToFit', viewTag);
        webViewMsgEntity.scalesPageToFit = scalesPageToFit;
        WebViewWorkerMsgHandler.workerPort.postMessage(webViewMsgEntity);
    }

    static deleteCookie(viewTag: number) : void {
        let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity('deleteCookie', viewTag);
        WebViewWorkerMsgHandler.workerPort.postMessage(webViewMsgEntity);
    }
}